/*
 * Copyright (c) 2010 Virtium Technology,
 * All Rights Reserved.
 * This work contains trade secrets and confidential material of
 * Virtium Technology.
 *
 * Warning: Any unauthorised dissemination, disclosure or use of the content
 * in whole or in part without the explicit consent of Virtium technology
 * will be deemed as intellectual property infindgement and the
 * offender will be subjected to legal actions.
 *
 * Project  : VPC
 * Name   : productMain.java
 * Author : NguyenTuanAnh
 *
 */
package com.virtium.vpc.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.virtium.vpc.utils.MailUtil;
import com.virtium.vpc.utils.ApplicationConstant;
import com.virtium.vpc.utils.StringUtil;
import com.virtium.vpc.action.controller.VpcConfigurationManager;
import com.virtium.vpc.action.controller.VpcDensityManager;
import com.virtium.vpc.action.controller.VpcFormfactorManager;
import com.virtium.vpc.action.controller.VpcHeightManager;
import com.virtium.vpc.action.controller.VpcModulerankManager;
import com.virtium.vpc.action.controller.VpcPackageManager;
import com.virtium.vpc.action.controller.VpcProductManager;
import com.virtium.vpc.action.controller.VpcProductstatusManager;
import com.virtium.vpc.action.controller.VpcRamdensityManager;
import com.virtium.vpc.action.controller.VpcRevisiontypeManager;
import com.virtium.vpc.action.controller.VpcSpeedManager;
import com.virtium.vpc.action.controller.VpcSsdramdensityManager;
import com.virtium.vpc.action.controller.VpcTechnologyManager;
import com.virtium.vpc.action.controller.VpcThicknessManager;
import com.virtium.vpc.form.productForm;
import com.virtium.vpc.hibernate.model.VpcProduct;
import com.virtium.vpc.hibernate.model.VpcPrototype;
import com.virtium.vpc.hibernate.model.VpcThickness;
import com.virtium.vpc.json.JSONArray;
import com.virtium.vpc.json.JSONObject;
import com.virtium.vpc.utils.FileUtil;

/**
 * @author NguyenTuanAnh AnhNguyen@viritum.com
 * 
 *         XDoclet definition:
 * @struts.action path="/productMain" name="productMain"
 *                input="/jsp/products.jsp" parameter="action" scope="request"
 */
public class productMain extends DispatchAction {
	/*
	 * Generated Methods
	 */
	private static Logger log = Logger.getLogger(productMain.class);
	
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){				
		return index(mapping,form,request,response);
	}

	public ActionForward index(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("index");
	}

	public ActionForward manager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("produtMgt");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		return mapping.findForward("search");
	}
	
	public ActionForward search2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int index = 1;
		String i = getPara(request, "i");
		try{
			index = Integer.parseInt(i);
		}catch(Exception ex){}
		productForm pForm = (productForm) form;
		pForm.setProductIndex(index);
		System.out.println("pForm: " + pForm.getProductIndex());
		return mapping.findForward("search2");
	}
	
	public ActionForward productDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("productDetail");
	}
	
	public ActionForward review(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("review");
	}
	
	
	
	
	public ActionForward addNewPro(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		try{
			JSONArray jarr = null;
			int type = Integer.parseInt(getPara(request, "proType"));
			String value =  getPara(request, "value");
			switch (type) {
			//Product Status
			case 1:
				VpcProductstatusManager.AddNew(value);				
				jarr = convertArrayToJSONObject(VpcProductstatusManager.getAll().toArray()); 
				break;
				//Technology
			case 2:
				VpcTechnologyManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcTechnologyManager.getAll().toArray());
				break;
				//Form Factor
			case 3:
				VpcFormfactorManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcFormfactorManager.getAll().toArray());
				break;
				//Density
			case 4:
				VpcDensityManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcDensityManager.getAll().toArray());
				break;
				//Height
			case 5:				
				VpcHeightManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcHeightManager.getAll().toArray());
				break;
				//Thickness
			case 6:
				String value2 = getPara(request, "value2");
				VpcThicknessManager.AddNew(value,value2);
				jarr = convertArrayToJSONObject2(VpcThicknessManager.getAll().toArray());
				break;
				//Speed
			case 7:
				VpcSpeedManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcSpeedManager.getAll().toArray());
				break;
				//DRam Density
			case 8:
				VpcRamdensityManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcRamdensityManager.getAll().toArray());
				break;
				//DRam Package
			case 9:
				VpcPackageManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcPackageManager.getAll().toArray());
				break;
				//DRam Configuration
			case 10:
				VpcConfigurationManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcConfigurationManager.getAll().toArray());
				break;
				//Module Rank
			case 11:
				VpcModulerankManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcModulerankManager.getAll().toArray());
				break;
				//SSDRAM Density
			case 12:
				VpcSsdramdensityManager.AddNew(value);
				jarr = convertArrayToJSONObject(VpcSsdramdensityManager.getAll().toArray());
				break;

			default:
				break;
			}
			
			response.setContentType("application/text");
			response.getWriter().write(jarr.toString());
		}catch(Exception ex){
			
		}
		return null;
	}
	
	public ActionForward deleteProperties(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		try{
			JSONArray jarr = null;
			int type = Integer.parseInt(getPara(request, "proType"));
			int id =  Integer.parseInt(getPara(request, "id"));
			switch (type) {
			//Product Status
			case 1:
				VpcProductstatusManager.Delete(id);				
				jarr = convertArrayToJSONObject(VpcProductstatusManager.getAll().toArray()); 
				break;
				//Technology
			case 2:
				VpcTechnologyManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcTechnologyManager.getAll().toArray());
				break;
				//Form Factor
			case 3:
				VpcFormfactorManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcFormfactorManager.getAll().toArray());
				break;
				//Density
			case 4:
				VpcDensityManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcDensityManager.getAll().toArray());
				break;
				//Height
			case 5:
				VpcHeightManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcHeightManager.getAll().toArray());
				break;
				//Thickness
			case 6:
				VpcThicknessManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcThicknessManager.getAll().toArray());
				break;
				//Speed
			case 7:
				VpcSpeedManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcSpeedManager.getAll().toArray());
				break;
				//DRam Density
			case 8:
				VpcRamdensityManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcRamdensityManager.getAll().toArray());
				break;
				//DRam Package
			case 9:
				VpcPackageManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcPackageManager.getAll().toArray());
				break;
				//DRam Configuration
			case 10:
				VpcConfigurationManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcConfigurationManager.getAll().toArray());
				break;
				//Module Rank
			case 11:
				VpcModulerankManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcModulerankManager.getAll().toArray());
				break;
			case 12:
				VpcSsdramdensityManager.Delete(id);
				jarr = convertArrayToJSONObject(VpcSsdramdensityManager.getAll().toArray());
				break;

			default:
				break;
			}
			
			response.setContentType("application/text");
			response.getWriter().write(jarr.toString());
		}catch(Exception ex){
			
		}
		return null;
	}
	
	
	

	public ActionForward searchProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
		
		List ls = doSearch(request);
		if(ls == null || ls.size() <=0) return null;
		arProduct.addAll(ls);
		String index = getPara(request, "index");
		try{
			response.setContentType("application/text");
			if(Integer.parseInt(index) < 4)
				response.getWriter().write(convertProductArrlayListToJSONArray(arProduct,false).toString());
			else
				response.getWriter().write(convertProductArrlayListToJSONArray(arProduct,true).toString());
		}catch(Exception ex){}
		
		return null;
	}
	
	
	
	public ActionForward getAllProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		JSONObject jarr = null;
		ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
		StringBuffer query = new StringBuffer("from VpcProduct product");
		query.append(" order by product.id desc ") ;
		List ls = VpcProductManager.queryString(query.toString());
		if(ls == null || ls.size() <=0) return null;
			arProduct.addAll(ls);
			
		jarr = convertProductArrlayListToJSONArray(arProduct,false);
		try{
			response.setContentType("application/text");
			response.getWriter().write(jarr.getJSONArray("data").toString());
		}catch(Exception ex){}
		
		return null;
	}
	public ActionForward updateStatusProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String value = getPara(request, "value");
		String id = getPara(request, "id");
		
		try{
			if(StringUtil.isEmpty(value) || StringUtil.isEmpty(id) )return null;
			
			if(Integer.parseInt(value) == 0){
				value = "2";
			}else value = "3";
			VpcProduct pro = VpcProductManager.getProductByID(Integer.parseInt(id));
			if(pro == null)return null;
			pro.setSpecstatusId(Integer.parseInt(value));
			response.setContentType("application/text");
			if(VpcProductManager.updateProduct(pro)){				
				response.getWriter().write("OK");}else{
				response.getWriter().write("ERROR");
			}
			
		}catch(Exception ex){}
		return null;
	}
	
	public ActionForward getAllCheckProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		JSONObject jarr = null;
		ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
		StringBuffer query = new StringBuffer("from VpcProduct product where product.specstatusId = 1");
		query.append(" order by product.id desc ") ;
		List ls = VpcProductManager.queryString(query.toString());
		if(ls == null || ls.size() <=0) return null;
			arProduct.addAll(ls);
			
		jarr = convertProductArrlayListToJSONArray(arProduct,false);
		try{
			response.setContentType("application/text");
			response.getWriter().write(jarr.getJSONArray("data").toString());
		}catch(Exception ex){}
		
		return null;
	}
	
	public ActionForward getAllRejectProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		JSONObject jarr = null;
		ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
		StringBuffer query = new StringBuffer("from VpcProduct product where product.specstatusId = 3");
		query.append(" order by product.id desc ") ;
		List ls = VpcProductManager.queryString(query.toString());
		if(ls == null || ls.size() <=0) return null;
			arProduct.addAll(ls);
			
		jarr = convertProductArrlayListToJSONArray(arProduct,false);
		try{
			response.setContentType("application/text");
			response.getWriter().write(jarr.getJSONArray("data").toString());
		}catch(Exception ex){}
		
		return null;
	}

	public ActionForward addNewProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String message = "Error";
		try {
			productForm prform = (productForm) form;			
			String filename1 = saveFile(request,prform.getObjectiveFile());
			String filename2 = saveFile(request,prform.getObjectiveFile2());			
					
			VpcProduct product = getParaProduct(request);
			product.setProductBrief(filename1);
			product.setDataSheet(filename2);
			
			// Set Defaul status --> Pendind
			product.setSpecstatusId(1);
			
			if(VpcProductManager.addNew(product)){
				message = "OK";
				//Send Mail
				String emailContent = FileUtil.readFile(FileUtil.virtiumServerPath + ApplicationConstant.get("tmpl.mail.addnewproduct.path"));
				String emailAdd ="";
				emailAdd = ApplicationConstant.get("mail.receive");
				MailUtil.sendEmail(emailAdd, emailAdd, "subject", emailContent);
			    	   
			}
			
			//response.setContentType("application/text");
			response.getWriter().write(message.toString());
			

		} catch (Exception ex) {
			System.out.println("Error: productMain - addNewProduct - ex: "
					+ ex.getMessage());			
		}		
		return null;
	}
	
	public ActionForward updateProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("Update Product bat dau");
		try {
			productForm prform = (productForm) form;
			VpcProduct product = getParaProduct(request);
			if(product.getId() == 0 || product.getId() == null)return null;
			
			String file = getPara(request, "file"); 
			String file2 = getPara(request, "file2");
			if(StringUtil.isEmpty(file))
				product.setProductBrief(saveFile(request,prform.getObjectiveFile()));
			else product.setProductBrief(file);
			
			if(StringUtil.isEmpty(file2))
				product.setDataSheet(saveFile(request,prform.getObjectiveFile2()));
			else product.setDataSheet(file2);
			
			// Set Defaul status --> Pending
			product.setSpecstatusId(1);
			String message = "Error";
			if(VpcProductManager.updateProduct(product)){
				message = "OK";
				//Send Mail
				String emailContent = FileUtil.readFile(FileUtil.virtiumServerPath + ApplicationConstant.get("tmpl.mail.addnewproduct.path"));
				String emailAdd ="";
				emailAdd = ApplicationConstant.get("mail.receive");
				if (MailUtil.sendEmail(emailAdd, emailAdd, "subject", emailContent))
			    	   log.debug("Email sent!");
			}
			
			//response.setContentType("application/text");
			response.getWriter().write(message.toString());
			

		} catch (Exception ex) {
			System.out.println("Error: productMain - addNewProduct - ex: "
					+ ex.getMessage());			
		}		
		return null;
	}
	
	public ActionForward searchProductMng(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		JSONObject jarr = null;
		String techID = getPara(request, "techID");
		String productStatusID = getPara(request, "productStatusID");
		String formfactorID = getPara(request, "formFactorID");		
		String densityID = getPara(request, "densityID");		
		String DRAMDensityID = getPara(request, "DRAMDensityID");
		String DRAMPackageID = getPara(request, "DRAMPackageID");
		String dramconfiguration = getPara(request, "DRAMConfigurationID");
		String moduleRankID = getPara(request, "moduleRankID");
		String moduleSpeedID = getPara(request, "moduleSpeedID");
		String cmHeight = getPara(request, "cmHeight");
		String cmThickness = getPara(request, "cmThickness");
		String revisionTypeId = getPara(request, "cmRevisionType");		
		String ruggedMemory = getPara(request, "ckRugged");		
		String highTemp = getPara(request, "ckHighTemp");
		String multichip = getPara(request, "ckMultiChip");		
		String ssdensity = getPara(request, "cmSSDRAMDensity");
		ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
		
		boolean isFirst = true;
		StringBuffer query = new StringBuffer("from VpcProduct product");
		if(techID != null && techID.length() >0){
			query.append(" where product.technologyId = " + techID);
			isFirst = false;
		}
		
		if(productStatusID != null && productStatusID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.productstatusId = " + productStatusID);
			isFirst = false;
		}
		
		if(densityID != null && densityID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.densityId = " + densityID);
			isFirst = false;
		}
		
		if(DRAMDensityID != null && DRAMDensityID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.ramdensityId = " + DRAMDensityID);
			isFirst = false;
		}
		
		if(DRAMPackageID != null && DRAMPackageID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.packageId = " + DRAMPackageID);
			isFirst = false;
		}
		
		if(formfactorID != null && formfactorID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.formFactorId = " + formfactorID);
			isFirst = false;
		}
		
		if(dramconfiguration != null && dramconfiguration.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.congigurationId = " + dramconfiguration);
			isFirst = false;
		}
		
		if(moduleRankID != null && moduleRankID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.modulerankId = " + moduleRankID);
			isFirst = false;
		}		
		
		if(moduleSpeedID != null && moduleSpeedID.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.speedId = " + moduleSpeedID);
			isFirst = false;
		}
		
		if(cmHeight != null && cmHeight.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.heightId = " + cmHeight);
			isFirst = false;
		}
		
		if(cmThickness != null && cmThickness.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.thicknessId = " + cmThickness);
			isFirst = false;
		}
		
		if(ssdensity != null && ssdensity.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.ssdramdensityId in (" + ssdensity + ")");
			isFirst = false;
		}		
		
		if(revisionTypeId != null && revisionTypeId.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.revisionTypeId in (" + revisionTypeId + ",3)");
			isFirst = false;
		}
		
		if(ruggedMemory != null && ruggedMemory.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.ruggedMemory = " + ruggedMemory) ;
			isFirst = false;
		}
		
		if(highTemp != null && highTemp.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.highTemp = " + highTemp) ;
			isFirst = false;
		}
		
		if(multichip != null && multichip.length() >0){
			query.append(" " + (isFirst ? "where":"and") + " product.multichip = " + multichip) ;
			isFirst = false;
		}
		
		query.append(" order by product.id desc ") ;
		
		
		List ls = VpcProductManager.queryString(query.toString());
		if(ls == null || ls.size() <=0) return null;
		arProduct.addAll(ls);
		jarr = convertProductArrlayListToJSONArray(arProduct,false);
		try{
			
			response.setContentType("application/text");
			response.getWriter().write(jarr.getJSONArray("data").toString());
			
		}catch(Exception ex){}
		
		return null;
	}	
	
	public ActionForward deleteProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
		String productID = getPara(request, "id");
		VpcProductManager.delete(productID);
		response.setContentType("application/text");
		response.getWriter().write("Ok");
		}catch(Exception ex){
			response.setContentType("application/text");
			response.getWriter().write(ex.getMessage());
		}
		return null;
	}
	
	public ActionForward getPropertiesList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
		//ArrayList<VpcTechnology> arrtech = VpcTechnologyManager.getAll();
		response.setContentType("application/text");
		response.getWriter().write("Ok");
		}catch(Exception ex){
			response.setContentType("application/text");
			response.getWriter().write(ex.getMessage());
		}
		return null;
	}
	
	public ActionForward checkPartNumber(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
			String pn = getPara(request, "pn");
			StringBuffer query = new StringBuffer("from VpcProduct product where product.pn='" + pn + "'");
			query.append(" order by product.id desc ") ;
			List ls = VpcProductManager.queryString(query.toString());
			response.setContentType("application/text");
			if(ls != null && ls.size() > 0)
				response.getWriter().write("OK");
			else response.getWriter().write("NO");
		
		}catch(Exception ex){
			response.setContentType("application/text");
			response.getWriter().write(ex.getMessage());
		}
		return null;
	}
	
	public ActionForward searchPart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
			ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
			JSONObject jarr = null;
			String pn = getPara(request, "query");
			StringBuffer query = new StringBuffer("from VpcProduct product where product.pn like '%" + pn + "%' and product.specstatusId = 2");
			query.append(" order by product.id desc ") ;
			List ls = VpcProductManager.queryString(query.toString());
			response.setContentType("application/text");
			if(ls != null && ls.size() > 0)
			{
				arProduct.addAll(ls);
				jarr = convertProductArrlayListToJSONArray(arProduct,false);
				try{					
					response.setContentType("application/text");
					JSONObject joTemp = new JSONObject();
					joTemp.put("ResultSet", jarr.getJSONArray("data"));
					response.getWriter().write(joTemp.toString());					
				}catch(Exception ex){}
			}
			
		
		}catch(Exception ex){
			response.setContentType("application/text");
			response.getWriter().write(ex.getMessage());
		}
		return null;
	}
	
	
	
	public ActionForward getContentMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
			String emailContent = FileUtil.readFile(FileUtil.virtiumServerPath + ApplicationConstant.get("tmpl.mail.path"));
			response.setContentType("application/text");
			response.getWriter().write(emailContent.toString());
		}catch(Exception ex){
			
		}
		return null;
	}
	
	public ActionForward sendmail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
			String emailContent = getPara(request, "text");
			String name = getPara(request, "name");
			String email = getPara(request, "email");
			String emailAdd ="";
			emailAdd = ApplicationConstant.get("mail.receive");
			if (MailUtil.sendEmail(email, emailAdd, "subject", emailContent))
		    	   log.debug("Email sent!");
			response.setContentType("application/text");
			response.getWriter().write(emailContent.toString());
		}catch(Exception ex){
			
		}
		return null;
	}
	
	public ActionForward getSubMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try{
			int index = Integer.parseInt(getPara(request, "index"));
			StringBuffer query = new StringBuffer("from VpcProduct product");
			ArrayList<VpcProduct> arProduct = new ArrayList<VpcProduct>();
			//&revisionTypeId=0&ruggedMemory=false&highTemp=false&multichip=false
			switch (index) {
				//DDR3
				case 0:
					query.append(" where product.technologyId = 1 and product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				//DDR2
				case 1:
					query.append(" where product.technologyId = 2 and product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				//DDR
				case 2:
					query.append(" where product.technologyId = 3 and product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				//SDRAM
				case 3:
					query.append(" where product.technologyId = 4 and product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				case 4:
					query.append(" where product.revisionTypeId in (2,3) and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				case 5:
					query.append(" where product.revisionTypeId in (1,3) and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				case 6:
					query.append(" where product.revisionTypeId =0 and product.ruggedMemory=true and product.highTemp = false and product.multichip = false");
				break;
				case 7:
					query.append(" where product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = true and product.multichip = false");
				break;
				case 8:
					query.append(" where product.technologyId in (5,6) and product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = false");
				break;
				case 9:
					query.append(" where product.revisionTypeId =0 and product.ruggedMemory=false and product.highTemp = false and product.multichip = true");
				break;
				
			}
			query.append(" order by product.id desc ") ;
			List ls = VpcProductManager.queryString(query.toString());
			if(ls == null || ls.size() <=0) return null;
			arProduct.addAll(ls);
			JSONObject jarr = convertProductArrlayListToJSONArray(arProduct,false);
			try{
				response.setContentType("application/text");
				response.getWriter().write(jarr.toString());
			}catch(Exception ex){}
			
			return null;
		}catch(Exception ex){
			
		}
		return null;
	}
	
	private VpcProduct getParaProduct(HttpServletRequest request) {
		VpcProduct vpcProduct = new VpcProduct();
		String temp = "";
		// ------ techID --------------------//
		String techID = getPara(request, "techID");
		// ------ techID --------------------//

		// ------ productStatusID --------------------//
		String productStatusID = getPara(request, "productStatusID");
		// ------ productStatusID --------------------//

		// ------ formFactorID --------------------//
		String formFactorID = getPara(request, "formFactorID");
		// ------ formFactorID --------------------//

		// ------ densityID --------------------//
		String densityID = getPara(request, "densityID");
		// ------ densityID --------------------//		

		// ------ DRAMDensityID --------------------//
		String DRAMDensityID = getPara(request, "DRAMDensityID");
		// ------ DRAMDensityID --------------------//

		// ------ DRAMPackageID --------------------//
		String DRAMPackageID = getPara(request, "DRAMPackageID");
		// ------ DRAMPackageID --------------------//

		// ------ DRAMConfigurationID --------------------//
		String DRAMConfigurationID = getPara(request, "DRAMConfigurationID");
		// ------ DRAMConfigurationID --------------------//
		
		// ------ moduleRankID --------------------//
		String moduleRankID = getPara(request, "moduleRankID");
		// ------ moduleRankID --------------------//		

		// ------ moduleSpeedID --------------------//
		String moduleSpeedID = getPara(request, "moduleSpeedID");
		// ------ moduleSpeedID --------------------//		

		// ------ parNumber --------------------//
		String parNumber = getPara(request, "parNumber");
		// ------ parNumber --------------------//
		
		String height = getPara(request, "cmHeight");
		String thickness = getPara(request, "cmThickness");
		
		if(!StringUtil.isEmpty(techID))
			vpcProduct.setTechnologyId(Integer.parseInt(techID));		
		if(!StringUtil.isEmpty(productStatusID))
			vpcProduct.setProductstatusId(Integer.parseInt(productStatusID));
		if(!StringUtil.isEmpty(formFactorID))
			vpcProduct.setFormFactorId(Integer.parseInt(formFactorID));
		if(!StringUtil.isEmpty(densityID))
			vpcProduct.setDensityId(Integer.parseInt(densityID));		
		if(!StringUtil.isEmpty(DRAMDensityID))
			vpcProduct.setRamdensityId(Integer.parseInt(DRAMDensityID));
		if(!StringUtil.isEmpty(DRAMPackageID))
			vpcProduct.setPackageId(Integer.parseInt(DRAMPackageID));
		if(!StringUtil.isEmpty(DRAMConfigurationID))
			vpcProduct.setCongigurationId(Integer.parseInt(DRAMConfigurationID));		
		if(!StringUtil.isEmpty(moduleRankID))
			vpcProduct.setModulerankId(Integer.parseInt(moduleRankID));		
		if(!StringUtil.isEmpty(parNumber))
			vpcProduct.setPn(parNumber);
		if(!StringUtil.isEmpty(moduleSpeedID))
			vpcProduct.setSpeedId(Integer.parseInt(moduleSpeedID));
		
		if(!StringUtil.isEmpty(height))
			vpcProduct.setHeightId(Integer.parseInt(height));
		
		if(!StringUtil.isEmpty(thickness))
			vpcProduct.setThicknessId(Integer.parseInt(thickness));
		
		if(!StringUtil.isEmpty(getPara(request, "cmRevisionType")))
			vpcProduct.setRevisionTypeId(Integer.parseInt(getPara(request, "cmRevisionType")));
		
		if(!StringUtil.isEmpty(getPara(request, "cmSSDRAMDensity")))
			vpcProduct.setSsdramdensityId(Integer.parseInt(getPara(request, "cmSSDRAMDensity")));
		
		if(!StringUtil.isEmpty(getPara(request, "ckRugged")))
			vpcProduct.setRuggedMemory(Integer.parseInt(getPara(request, "ckRugged"))==1);
		
		if(!StringUtil.isEmpty(getPara(request, "ckHighTemp")))
			vpcProduct.setHighTemp(Integer.parseInt(getPara(request, "ckHighTemp"))==1);
		
		if(!StringUtil.isEmpty(getPara(request, "ckMultiChip")))
			vpcProduct.setMultichip(Integer.parseInt(getPara(request, "ckMultiChip"))==1);
		
		if(!StringUtil.isEmpty(getPara(request, "id")))
			vpcProduct.setId(Integer.parseInt(getPara(request, "id")));
		
		return vpcProduct;
	}

	private String getPara(HttpServletRequest request, String id) {
		String temp = "";
		try {
			temp = request.getParameter(id);

			if (temp != null && !temp.trim().equals(""))
				temp = temp.toString();
			else
				temp = "";
		} catch (Exception ex) {
			System.out.println("Error: ProductMain - getPara: "
					+ ex.getMessage());
		}

		return temp;
	}
	private String getColorFromTechID(int index){
		String color = "";
		switch (index) {
		case 0:
			color =  "black";
			break;
		case 1:
			color =  "#A349A4";
			break;
		case 2:
			color =  "#AD1400";
			break;
		case 3:
			color =  "#D6AE00";
			break;
		case 4:
			color =  "#109618";
			break;
		case 5:
			color =  "#FF8000";
			break;
		case 6:
			color =  "#000040";
			break;
		default:
			color =  "black";
			break;
		}
		return color;
		
	}
	private JSONObject convertProductArrlayListToJSONArray(ArrayList<VpcProduct> arProduct, boolean isMakeColor){
		JSONArray jarr = null;
		JSONArray jFfactor = new JSONArray();
		JSONArray jDensity = new JSONArray();
		JSONArray jTech = new JSONArray();		
		JSONArray jDRamConfig = new JSONArray();
		JSONArray jSSDen = new JSONArray();
		
		ArrayList<String> arFF = new ArrayList<String>();
		ArrayList<String> arDen = new ArrayList<String>();		
		ArrayList<String> artech = new ArrayList<String>();
		ArrayList<String> arDRamConfig = new ArrayList<String>();
		ArrayList<String> arSSDen = new ArrayList<String>();
		
		int indexColor = 0;
		JSONObject jObject = new JSONObject();
		try {
			if (arProduct.size() > 0) {
				jarr = new JSONArray();
				for (int i = 0; i < arProduct.size(); i++) {
					try {
						VpcProduct row = arProduct.get(i);					
											
						//Tech
						if(!artech.contains(row.getTechnologyId().toString())){
							jTech.put(isMakeColor ? ConvertTech(row).put("color",getColorFromTechID(row.getTechnologyId())) : ConvertTech(row));
							artech.add(row.getTechnologyId().toString());
							if(isMakeColor) indexColor++;
						}
						
						//FormFactor
						if(!arFF.contains(row.getFormFactorId().toString())){					
							jFfactor.put(isMakeColor ? ConvertFormFactor(row).put("color", getColorFromArrTech(jTech,row.getTechnologyId().toString())) : ConvertFormFactor(row));
							arFF.add(row.getFormFactorId().toString());
							
						}else if(isMakeColor && !(row.getTechnologyId().toString().equals(getObjectByID(jFfactor,row.getFormFactorId().toString()).get("tech").toString()))){
							jFfactor = makeMultiTechColor(jFfactor,row.getFormFactorId().toString());
						}
						
						//Density
						if(!arDen.contains(row.getDensityId().toString())){
							jDensity.put(isMakeColor ? ConvertDensity(row).put("color", getColorFromArrTech(jTech,row.getTechnologyId().toString())) : ConvertDensity(row));
							arDen.add(row.getDensityId().toString());
						}else if(isMakeColor && !(row.getTechnologyId().toString().equals(getObjectByID(jDensity,row.getDensityId().toString()).get("tech").toString()))){
							jDensity = makeMultiTechColor(jDensity,row.getDensityId().toString());
						}
						
						//Config
						if(!arDRamConfig.contains(row.getCongigurationId().toString())){
							jDRamConfig.put(isMakeColor ? ConvertDramConfig(row).put("color", getColorFromArrTech(jTech,row.getTechnologyId().toString())) : ConvertDramConfig(row));
							arDRamConfig.add(row.getCongigurationId().toString());
						}else if(isMakeColor && !(row.getTechnologyId().toString().equals(getObjectByID(jDRamConfig,row.getCongigurationId().toString()).get("tech").toString()))){
							jDRamConfig = makeMultiTechColor(jDRamConfig,row.getCongigurationId().toString());
						}
						
						//SSDen
						if(!arSSDen.contains(row.getSsdramdensityId().toString())){
							jSSDen.put(isMakeColor ? ConvertSSDen(row).put("color", getColorFromArrTech(jTech,row.getTechnologyId().toString())) : ConvertSSDen(row));
							arSSDen.add(row.getSsdramdensityId().toString());
						}else if(isMakeColor && !(row.getTechnologyId().toString().equals(getObjectByID(jSSDen,row.getSsdramdensityId().toString()).get("tech").toString()))){
							jSSDen = makeMultiTechColor(jSSDen,row.getSsdramdensityId().toString());
						}
						
						
						
						JSONObject joTemp = new JSONObject();
						
						joTemp.put("id", row.getId().toString());
						joTemp.put("techID", row.getTechnologyId() == null ? "0" : row.getTechnologyId()
								.toString());
						joTemp.put("tech",row.getTechnologyId() == null ? "" :
								VpcTechnologyManager.getTextByID(arProduct.get(
										i).getTechnologyId()));

						joTemp.put("productStatusID", row
								.getProductstatusId() == null ? "0" : row
								.getProductstatusId().toString());
						joTemp.put("productStatus", row
								.getProductstatusId() == null ? "" : VpcProductstatusManager
								.getTextByID(row
										.getProductstatusId()));

						joTemp.put("formFactorID", row
								.getFormFactorId() == null ? "0" : row
								.getFormFactorId().toString());
						joTemp.put("formFactor", row
								.getFormFactorId() == null ? "" : 
								VpcFormfactorManager.getTextByID(arProduct.get(
										i).getFormFactorId()));

						joTemp.put("densityID", row
								.getDensityId() == null ? "0" : row.getDensityId()
								.toString());
						joTemp.put("density", row
								.getDensityId() == null ? "" : VpcDensityManager
								.getTextByID(row.getDensityId()));						

						joTemp.put("DRAMDensityID", row
								.getRamdensityId() == null ? "" : row
								.getRamdensityId().toString());
						joTemp.put("DRAMDensity", row
								.getRamdensityId() == null ? "" : 
								VpcRamdensityManager.getTextByID(arProduct.get(
										i).getRamdensityId()));

						joTemp.put("DRAMPackageID", row
								.getPackageId() == null ? "" : row
								.getPackageId().toString());
						joTemp.put("DRAMPackage", row
								.getPackageId() == null ? "" : VpcPackageManager
								.getTextByID(row.getPackageId()));

						joTemp.put("DRAMConfigurationID", row
								.getCongigurationId() == null ? "" : row
								.getCongigurationId().toString());
						joTemp.put("DRAMConfiguration", row
								.getCongigurationId() == null ? "" : VpcConfigurationManager
								.getTextByID(row
										.getCongigurationId()));						

						joTemp.put("moduleRankID", row
								.getModulerankId() == null ? "" : row
								.getModulerankId().toString());
						joTemp.put("moduleRank", row
								.getModulerankId() == null ? "" : 
								VpcModulerankManager.getTextByID(arProduct.get(
										i).getModulerankId()));						

						joTemp.put("parNumber", row.getPn()
								.toString());

						joTemp.put("moduleSpeedID", row
								.getSpeedId() == null ? "" : row
								.getSpeedId().toString());
						joTemp.put("moduleSpeed", row
								.getSpeedId() == null ? "" : VpcSpeedManager
								.getTextByID(row
										.getSpeedId()));
						
						joTemp.put("heightID", row
								.getHeightId() == null ? "" : row
								.getHeightId().toString());
						joTemp.put("height", row
								.getHeightId() == null ? "" : VpcHeightManager
								.getTextByID(row
										.getHeightId()));
						
						joTemp.put("thicknessID", row
								.getThicknessId() == null ? "" : row
								.getThicknessId().toString());
						joTemp.put("thickness", row
								.getThicknessId() == null ? "" : VpcThicknessManager
								.getTextByID(row
										.getThicknessId()));
						
						joTemp.put("ssddensityID", row
								.getSsdramdensityId() == null ? "" : row
								.getSsdramdensityId().toString());
						joTemp.put("ssddensity", row
								.getSsdramdensityId() == null ? "" : VpcSsdramdensityManager
								.getTextByID(row
										.getSsdramdensityId()));
						
						joTemp.put("file2", row
								.getDataSheet());
						joTemp.put("file", row
								.getProductBrief());
						
						joTemp.put("revisionTypeId", row
								.getRevisionTypeId() == null ? "" : row
										.getRevisionTypeId());
						joTemp.put("revisionType", row
								.getRevisionTypeId() == null ? "" : VpcRevisiontypeManager.getTextByID(row
										.getRevisionTypeId()));
						joTemp.put("rugged", row
								.getRuggedMemory() == null ? "" : row
										.getRuggedMemory().toString());
						joTemp.put("highTemp", row
								.getHighTemp() == null ? "" : row
										.getHighTemp().toString());
						joTemp.put("Multi", row
								.getMultichip() == null ? "" : row
										.getMultichip().toString());

						jarr.put(joTemp);
					} catch (Exception ex) {
						continue;
					}
				}
				jObject.put("data", jarr);				
				jObject.put("ff", jFfactor);
				jObject.put("density", jDensity);
				jObject.put("tech", jTech);
				jObject.put("config", jDRamConfig);
				jObject.put("ss", jSSDen);
				return jObject;

			}
		} catch (Exception ex) {
		}
		return null;
	}
	
	private JSONArray sortAlphabe(JSONArray object){
		ArrayList<JSONObject> newObject = new ArrayList<JSONObject>();
		JSONArray newJObject = new JSONArray();
		try{
			for(int i =0; i < object.length(); i++){
				JSONObject t =  (JSONObject)object.get(i);
				newObject.add(compareJSON(t,newObject), t);
			}
			for(int i=0; i <  newObject.size();i++)
				newJObject.put((JSONObject)newObject.get(i));
			return newJObject;
			
		}catch(Exception ex){}
		return null;
	}
	
	private int compareJSON(JSONObject t, ArrayList<JSONObject> object){		
		try{
			for(int i=0;i < object.size(); i++){
				JSONObject s = (JSONObject)object.get(i);
				if(((String)s.get("text")).compareToIgnoreCase((String)t.get("text")) == -1)
						return i;
			}
		}catch(Exception ex){}
		return 0;
	}
	
	private String getColorFromArrTech(JSONArray JArTech, String techId){
		try{
			for(int i =0; i < JArTech.length(); i++){
				JSONObject j = (JSONObject)JArTech.get(i);
				if(j.get("id").toString().equals(techId.toString()))
					return (String)j.get("color");
			}
			//getObjectByID(JArTech,techId).get("color").toString();
		}catch(Exception ex){}
		return "black";
	}
	
	private JSONObject getObjectByID(JSONArray objects, String id){
		try{
			for(int i =0; i < objects.length(); i++){
				JSONObject j = (JSONObject)objects.get(i);
				if(j.get("id").toString().equals(id.toString()))
					return j;
			}
		}catch(Exception ex){}
		return null;
	}
	
	private JSONArray makeMultiTechColor(JSONArray current, String id){
		try{
			for(int i =0; i < current.length(); i++){
				JSONObject object = (JSONObject)current.get(i);
				if(object.get("id").toString().equals(id.toString()))					
					return current.put(i, object.put("color", "black"));
			}
		}catch(Exception ex){}
		return current;
	}
	
	private String saveFile(HttpServletRequest request,FormFile myFile){
		String fileName = "";
		if (myFile != null) {				
			try{
				fileName = myFile.getFileName();
				System.out.println("Save file FileName: " + fileName);
				// get data
				byte[] fileData = myFile.getFileData();
				// save this data to server dataSheets folder: try to use it
				// from config
				String physicalWebAppPath = request.getSession()
						.getServletContext().getRealPath("/");
				System.out.println("physicalWebAppPath: " + physicalWebAppPath);

				String dataSheetFolder = physicalWebAppPath + "dataSheets";
				String fullFileName = dataSheetFolder + "/" + fileName;
				File dat = new File(fullFileName);
				if (!dat.isDirectory() && fileData.length > 0) {
					FileUtil
							.uploadFile(dataSheetFolder, fullFileName, fileData);
				}
			}catch(Exception ex){
				System.out.println("Save file: " + ex.getMessage());
			}
		}
		return fileName;
	
	}
	
	public ActionForward export(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		System.out.println("Export: 1");
		List listproduct = doSearch(request);
		System.out.println("Export: 2");
		listproduct = getLinesExportToCsv(listproduct);
		System.out.println("Export: 3");
		
		// write List to .csv file
		String fileName = "virtium_report.xls";
		String filePath = FileUtil.virtiumServerPath;
		System.out.println("Export: 4 - filePath: " + filePath);
		filePath = filePath + "/reports/" + fileName;
		writeListToFile(listproduct, filePath);
		System.out.println("Export: 5");
		// push file to user via response
		boolean isDownloaded = FileUtil.downloadFile(filePath, fileName,
				response);
		System.out.println("Export: 6");
		System.out.println("===== File successfully downloaded: filePath = " + filePath
				+ " : " + isDownloaded);
		return null;
	}
	
	private void writeListToFile(List listCompetency, String filePath) {
		// Stream to write file
		FileOutputStream fout;

		try {
			/*Workbook wb = new HSSFWorkbook();
		    
		    CreationHelper createHelper = wb.getCreationHelper();
		    Sheet sheet = wb.createSheet("Virtium - Report");
		    
		    for (int i = 0; i < listCompetency.size(); i++) {
		    	// Create a row and put some cells in it. Rows are 0 based.
			    Row row = sheet.createRow((short)0);
			    // Create a cell and put a value in it.
			    Cell cell = row.createCell(0);
			    cell.setCellValue(1);

			    // Or do it on one line.
			    row.createCell(1).setCellValue(1.2);
			    row.createCell(2).setCellValue(
			         createHelper.createRichTextString("This is a string"));
			    row.createCell(3).setCellValue(true);
			}
		    

		    // Write the output to a file
		    fout = new FileOutputStream(filePath);
		    wb.write(fout);
		    fout.close();
		    */
			// Open an output stream
			fout = new FileOutputStream(filePath);

			String lines = "", aLine = "";
			for (int i = 0; i < listCompetency.size(); i++) {
				aLine = (String) listCompetency.get(i);
				lines += aLine + "\n";
			}

			// Print a line of text
			new PrintStream(fout).println(lines);

			// Close our output stream
			fout.close();
		}
		// Catches any error conditions
		catch (IOException e) {
			System.err.println("Unable to write to file");
			System.exit(-1);
		}

	}
	
	private List getLinesExportToCsv(List product){
		List result = new ArrayList();
		try{
			StringBuffer header = new StringBuffer();
			header.append("<table border=\"1\"><thead><tr>");
			header.append("<th>P/N</th>");
			header.append("<th>Technology</th>");
			header.append("<th>Product Status</th>");
			header.append("<th>Form factor</th>");
			header.append("<th>Density</th>");
			header.append("<th>Hieght(mm)</th>");
			header.append("<th>Thickness(mm)</th>");
			header.append("<th>Speed</th>");
			header.append("<th>Dram Density</th>");
			header.append("<th>Dram Package</th>");
			header.append("<th>Dram Config</th>");
			header.append("<th>Module Rank</th></tr>");
			result.add(header.toString());
			
			Iterator<VpcProduct> iter = product.listIterator();
			while(iter.hasNext()){
				header = new StringBuffer();
				header.append("<tr>");
				VpcProduct pro = iter.next();
				header.append("<td>" + pro.getPn() + "</td>");
				header.append("<td>" + VpcTechnologyManager.getTextByID(pro.getTechnologyId()) + "</td>");
				header.append("<td>" + VpcProductstatusManager.getTextByID(pro.getProductstatusId()) + "</td>");
				header.append("<td>" + VpcFormfactorManager.getTextByID(pro.getFormFactorId()) + "</td>");
				header.append("<td>" + VpcDensityManager.getTextByID(pro.getDensityId()) + "</td>");
				header.append("<td>" + VpcHeightManager.getTextByID(pro.getHeightId()) + "</td>");
				header.append("<td>" + VpcThicknessManager.getTextByID(pro.getThicknessId()) + "</td>");
				header.append("<td>" + VpcSpeedManager.getTextByID(pro.getSpeedId()) + "</td>");
				header.append("<td>" + VpcRamdensityManager.getTextByID(pro.getRamdensityId()) + "</td>");
				header.append("<td>" + VpcPackageManager.getTextByID(pro.getPackageId()) + "</td>");
				header.append("<td>" + VpcConfigurationManager.getTextByID(pro.getCongigurationId()) + "</td>");
				header.append("<td>" + VpcModulerankManager.getTextByID(pro.getModulerankId()) + "</td>");
				header.append("</tr>");
				result.add(header.toString());
			}
			header.append("</thead></table>");
		}catch(Exception ex){}
		return result;
	}
	
	private JSONObject ConvertFormFactor(VpcProduct object){
		JSONObject jTemp = new JSONObject();
		try{
			jTemp.put("id", object.getFormFactorId().toString());
			jTemp.put("text", VpcFormfactorManager.getTextByID(object.getFormFactorId()));
			jTemp.put("color", "black");
			jTemp.put("tech", object.getTechnologyId().toString());
		}catch(Exception ex){}
		return jTemp;
	}
	
	private JSONObject ConvertDensity(VpcProduct object){
		JSONObject jTemp = new JSONObject();
		try{
			jTemp.put("id", object.getDensityId().toString());
			jTemp.put("text", VpcDensityManager.getTextByID(object.getDensityId()));
			jTemp.put("color", "black");
			jTemp.put("tech", object.getTechnologyId().toString());
		}catch(Exception ex){}
		return jTemp;
	}
	
	private JSONObject ConvertTech(VpcProduct object){
		JSONObject jTemp = new JSONObject();
		try{
			jTemp.put("id", object.getTechnologyId().toString());
			jTemp.put("text", VpcTechnologyManager.getTextByID(object.getTechnologyId()));
			jTemp.put("color", "black");
			jTemp.put("tech", object.getTechnologyId().toString());
		}catch(Exception ex){}
		return jTemp;
	}
	
	private JSONObject ConvertDramConfig(VpcProduct object){
		JSONObject jTemp = new JSONObject();
		try{
			jTemp.put("id", object.getCongigurationId().toString());
			jTemp.put("text", VpcConfigurationManager.getTextByID(object.getCongigurationId()));
			jTemp.put("color", "black");
			jTemp.put("tech", object.getTechnologyId().toString());
		}catch(Exception ex){}
		return jTemp;
	}
	
	private JSONObject ConvertSSDen(VpcProduct object){
		JSONObject jTemp = new JSONObject();
		try{
			jTemp.put("id", object.getSsdramdensityId().toString());
			jTemp.put("text", VpcSsdramdensityManager.getTextByID(object.getSsdramdensityId()));
			jTemp.put("color", "black");
			jTemp.put("tech", object.getTechnologyId().toString());
		}catch(Exception ex){}
		return jTemp;
	}
	
	private JSONArray convertArrayToJSONObject(Object arrObject[]){
		JSONArray jarr = null;
		try{
			if(arrObject != null && arrObject.length > 0){
				jarr = new JSONArray();
				for(int i =0;i < arrObject.length;i++){
					JSONObject JObject = new JSONObject();
					JObject.put("id", ((VpcPrototype)arrObject[i]).getId());
					JObject.put("text", ((VpcPrototype)arrObject[i]).getText());
					jarr.put(JObject);
				}
				return jarr;
			}
		}catch(Exception ex){}
		return null;
	}	
	
	
	private JSONArray convertArrayToJSONObject2(Object arrObject[]){
		JSONArray jarr = null;
		try{
			if(arrObject != null && arrObject.length > 0){
				jarr = new JSONArray();
				for(int i =0;i < arrObject.length;i++){
					JSONObject JObject = new JSONObject();
					JObject.put("id", ((VpcThickness)arrObject[i]).getId());
					JObject.put("text", ((VpcThickness)arrObject[i]).getText());
					JObject.put("text2", ((VpcThickness)arrObject[i]).getText2());
					jarr.put(JObject);
				}
				return jarr;
			}
		}catch(Exception ex){}
		return null;
	}
	
	private List doSearch(HttpServletRequest request){
		String techIDs = getPara(request, "techIDs");
		String densityIDs = getPara(request, "densityIDs");
		String formfactorIDs = getPara(request, "formfactorIDs");
		String revisionTypeId = getPara(request, "revisionTypeId");
		String ruggedMemory = getPara(request, "ruggedMemory");
		String highTemp = getPara(request, "highTemp");
		String multichip = getPara(request, "multichip");
		String dramconfiguration = getPara(request, "dramconfiguration");
		String ssdensity = getPara(request, "ssdensity");
		
		
		boolean isFirst = true;
		StringBuffer query = new StringBuffer("from VpcProduct product");
		if(techIDs != null && techIDs.length() >0){
			query.append(" where product.technologyId in (" + techIDs + ")");
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(densityIDs)){
			query.append(" " + (isFirst ? "where":"and") + " product.densityId in (" + densityIDs + ")");
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(formfactorIDs)){
			query.append(" " + (isFirst ? "where":"and") + " product.formFactorId in (" + formfactorIDs + ")");
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(dramconfiguration)){
			query.append(" " + (isFirst ? "where":"and") + " product.congigurationId in (" + dramconfiguration + ")");
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(ssdensity)){
			query.append(" " + (isFirst ? "where":"and") + " product.ssdramdensityId in (" + ssdensity + ")");
			isFirst = false;
		}		
		
		if(!StringUtil.isEmpty(revisionTypeId)){
			query.append(" " + (isFirst ? "where":"and") + " product.revisionTypeId in (" + revisionTypeId + ",3)");
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(ruggedMemory)){
			query.append(" " + (isFirst ? "where":"and") + " product.ruggedMemory = " + ruggedMemory) ;
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(highTemp)){
			query.append(" " + (isFirst ? "where":"and") + " product.highTemp = " + highTemp) ;
			isFirst = false;
		}
		
		if(!StringUtil.isEmpty(multichip)){
			query.append(" " + (isFirst ? "where":"and") + " product.multichip = " + multichip) ;
			isFirst = false;
		}
		
		query.append(" " + (isFirst ? "where":"and") + " product.specstatusId = 2 order by product.technologyId asc ") ;
		return VpcProductManager.queryString(query.toString());
	}
}